package com.fg.cloud.manager.service.agency.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.finals.Agency;
import com.fg.cloud.manager.entry.BaseEntity;
import com.fg.cloud.manager.entry.agency.AgencyProductEntity;
import com.fg.cloud.manager.entry.agency.vo.AgencyProductVo;
import com.fg.cloud.manager.mapper.BaseMapper;
import com.fg.cloud.manager.mapper.agency.AgencyProductMapper;
import com.fg.cloud.manager.service.BaseServiceImpl;
import com.fg.cloud.manager.service.agency.AgencyPriceService;

/**
 * Description:经销商特价产品
 * @author around
 * @date 2017年8月3日上午11:49:06
 */
@Service
public class AgencyPriceServiceImpl extends BaseServiceImpl implements AgencyPriceService {

	@Autowired
	private AgencyProductMapper agencyPriceMapper;
	
	@Override
	protected <T extends BaseEntity> BaseMapper getBaseMapper() {
		return agencyPriceMapper;
	}

	@Override
	public void findList(PageUtils<AgencyProductVo> pageInfo) {
		long count = agencyPriceMapper.findCountByAgencyPrice(pageInfo);
		pageInfo.setTotal(count);
		if(count > 0) {
			pageInfo.setData(agencyPriceMapper.findByAgencyPrice(pageInfo));
		} else {
			pageInfo.setData(new ArrayList<AgencyProductVo>());
		}
	}

	@Override
	@Transactional
	public boolean deletes(Long facId, Long ageId, Long[] ids) {
		return agencyPriceMapper.deletes(Agency.getTableName(facId), facId, ageId, ids, 1) > 0 ? true : false;
	}

	@Override
	public void select(PageUtils<AgencyProductVo> pageInfo) {
		long count = agencyPriceMapper.findSelectCountByAgencyPrice(pageInfo);
		pageInfo.setTotal(count);
		if(count > 0) {
			pageInfo.setData(agencyPriceMapper.findSelectByAgencyPrice(pageInfo));
		} else {
			pageInfo.setData(new ArrayList<AgencyProductVo>());
		}
	}

	@Override
	@Transactional
	public boolean insert(Long facId, List<AgencyProductEntity> list) {
		return agencyPriceMapper.batchInsertByAgencyPrice(
				Agency.getTableName(facId), list) > 0 ? true : false;
	}

	@Override
	public boolean update(AgencyProductEntity product) {
		return agencyPriceMapper.updatePrice(Agency.getTableName(product.getFacId()),
				product.getId(), product.getPrice()) > 0 ? true :false;
	}

}
